package com.xzj.spring.annotation;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @ProjectName: SpringDemo
 * @Package: com.xzj.spring.annotation
 * @ClassName: ResortSeqDemo
 * @Author: xzj
 * @Description:
 * @Date: 2022/7/20 14:13
 * @Version: 1.0
 */
public class ResortSeqDemo {
    private int x = 0;
    private int y = 0;

    public void method01() {
        x = 1;
        y = 5;
    }

    public void method02() {
        int a = y;
        int b = x;
        System.out.println(String.format("a:%s,b:%s", a, b));
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(200, 200,
                100, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1000),
                new ThreadFactory() {
                    @Override
                    public Thread newThread(Runnable r) {
                        //创建线程池中的线程
                        Thread thread = new Thread(r);
                        //设置线程名称
                        thread.setName("Thread-" + r.hashCode());
                        //设置线程优先级（最大值：10）
                        thread.setPriority(Thread.MAX_PRIORITY);
                        //......
                        return thread;
                    }
                });
        int i = 0;
        while (i < 10000) {
            Thread.sleep(1);
            i++;
            ResortSeqDemo resortSeqDemo = new ResortSeqDemo();
            threadPoolExecutor.execute(resortSeqDemo::method01);
            threadPoolExecutor.execute(resortSeqDemo::method02);
        }
    }
}
